package com.healer.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.healer.dao.BookMapper;
import com.healer.pojo.Book;
import com.healer.pojo.BookBorrowing;
import com.healer.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public void addBook(Map map) {
        bookMapper.addBook(map);
    }

    @Override
    public void editBook(Map map) {
        bookMapper.editBook(map);
    }

    @Override
    public void deleteBook(Map map) {
        bookMapper.deleteBook(map);
    }

    @Override
    public Map queryBookList(Map map) {
//     当前页
        int pageNum=Integer.parseInt(map.get("pageNum").toString());
//      页长
        int pageSize=Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(pageNum,pageSize);
        List<Book> bookList=bookMapper.queryBookList(map);
        PageInfo pageInfo=new PageInfo(bookList);
        long total=pageInfo.getTotal();
        bookList.forEach(e-> System.out.println(e.toString()));
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("total",total);
        resultMap.put("rows",bookList);

        return resultMap;
    }

    @Override
    public void addBorrowingBook(Map map) {
        bookMapper.addBorrowingBook(map);
    }

    @Override
    public void returnBorrowingBook(Map map) {
        bookMapper.returnBorrowingBook(map);
    }

    @Override
    public Map queryBorrowing(Map map) {
        //     当前页
        int pageNum=Integer.parseInt(map.get("pageNum").toString());
//      页长
        int pageSize=Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(pageNum,pageSize);
        List<BookBorrowing> bookBorrowings = bookMapper.queryBorrowing(map);
        PageInfo pageInfo=new PageInfo(bookBorrowings);
        long total=pageInfo.getTotal();
        bookBorrowings.forEach(e-> System.out.println(e.toString()));
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("total",total);
        resultMap.put("rows",bookBorrowings);
        System.out.println(resultMap);
        return resultMap;
    }

    @Override
    public void updateInventories(Map map) {
        bookMapper.updateInventories(map);
    }

}
